#include "GameState.h"

#include "Config.h"

GameState::GameState(const Config& ConfigInstance)
    : MapInstance(Map(ConfigInstance.Height, std::vector<ObjectType>(ConfigInstance.Width, EMPTY)))
    , PlayerCoord(ConfigInstance.PlayerCoord)
    , EndZoneCoords(ConfigInstance.EndZoneCoords)
{
    MapInstance[ConfigInstance.PlayerCoord.first][ConfigInstance.PlayerCoord.second] = PLAYER;

    for (const Coord2D& Coord : ConfigInstance.WallCoords)
    {
        MapInstance[Coord.first][Coord.second] = WALL;
    }

    for (const Coord2D& Coord : ConfigInstance.BoxCoords)
    {
        MapInstance[Coord.first][Coord.second] = BOX;
    }

    for (const Coord2D& Coord : ConfigInstance.EndZoneCoords)
    {
        MapInstance[Coord.first][Coord.second] = ENDZONE;
    }
}

bool GameState::IsInEndZone(const Coord2D& Coord) const
{
    for (const Coord2D& EndZoneCoord : EndZoneCoords)
    {
        if (Coord == EndZoneCoord)
        {
            return true;
        }
    }
    return false;
}

bool GameState::IsInCorner(const Coord2D& Coord) const
{
    return IsOutOfRange(Coord)
        || MapInstance[Coord.first][Coord.second] == WALL
        || (MapInstance[Coord.first - 1][Coord.second] == WALL && MapInstance[Coord.first][Coord.second - 1] == WALL)
        || (MapInstance[Coord.first - 1][Coord.second] == WALL && MapInstance[Coord.first][Coord.second + 1] == WALL)
        || (MapInstance[Coord.first + 1][Coord.second] == WALL && MapInstance[Coord.first][Coord.second - 1] == WALL)
        || (MapInstance[Coord.first + 1][Coord.second] == WALL && MapInstance[Coord.first][Coord.second + 1] == WALL);
}
